package dxn.nl.file.api;

import dxn.boot.common.entity.AuthUser;
import dxn.boot.common.entity.BaseResult;
import dxn.boot.common.entity.CsBasePageQuery;
import dxn.boot.common.entity.CsBasePageResult;
import dxn.boot.starter.web.control.BaseControl;
import dxn.boot.starter.web.security.AuthUtils;
import dxn.nl.file.service.CsImportFileService;
import dxn.nl.file.biz.po.FileImportFileDetail;
import dxn.nl.file.biz.vo.CsImportFileBaseVo;
import dxn.nl.file.biz.vo.CsImportFileVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/cs/import")
@Tag(name = "Excel导入管理")
public class CsImportControl extends BaseControl {

    @Resource
    private CsImportFileService csImportFileService;

    @Operation(summary = "分页查询导入文件")
    @RequestMapping(value = "/pageVo", method = RequestMethod.POST)
    public BaseResult<CsBasePageResult<FileImportFileDetail>> pageVo(@RequestBody @Valid CsBasePageQuery<CsImportFileVo> pageQuery) {

        AuthUser userDetails = AuthUtils.getCurrentUser();

        if(pageQuery.getQuery() == null)
        {
            return BaseResult.fail("请求内容不能为空");
        }

        CsBasePageResult<FileImportFileDetail> result = csImportFileService.pageVo(pageQuery);

        return BaseResult.success(result);
    }

    @Operation(summary = "导入文件")
    @RequestMapping(value = "/importFile", method = RequestMethod.POST)
    public BaseResult<String> importYtoTransportPolicyFee(@RequestParam("file") MultipartFile file,
                                                          @RequestParam("fileType") String fileType)
    {
        log.info("kaihsi "+file.getOriginalFilename());

        AuthUser userDetails = AuthUtils.getCurrentUser();
        String result = csImportFileService.importFile(file,fileType,userDetails);

        log.info("jieshu "+file.getOriginalFilename());
        return checkAndReturnResult(result);
    }

    @Operation(summary = "重新处理")
    @RequestMapping(value = "/reDeal", method = RequestMethod.POST)
    public BaseResult<String> reDeal(@RequestBody @Valid CsImportFileBaseVo vo) {

        AuthUser userDetails = AuthUtils.getCurrentUser();

        String result = csImportFileService.pushMq(vo);

        return checkAndReturnResult(result);
    }

    @Operation(summary = "删除")
    @RequestMapping(value = "/deleteFile", method = RequestMethod.POST)
    public BaseResult<String> deleteFile(@RequestBody @Valid CsImportFileBaseVo vo) {

        AuthUser userDetails = AuthUtils.getCurrentUser();

        String result = csImportFileService.deleteFile(vo);

        return checkAndReturnResult(result);
    }

    @Operation(summary = "修改导入文件状态")
    @RequestMapping(value = "/setStatus", method = RequestMethod.POST)
    public BaseResult<String> setStatus(@RequestBody @Valid CsImportFileBaseVo vo) {

        AuthUser userDetails = AuthUtils.getCurrentUser();

        String result = "暂未开通";//csImportFileService.deleteFile(vo);

        return checkAndReturnResult(result);
    }
}